package com.findviewbydream.evil.helloyouth.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.findviewbydream.evil.helloyouth.Fragment.HomeFragmentContent.CommunionFragment;
import com.findviewbydream.evil.helloyouth.Fragment.HomeFragmentContent.SecondHandFragment;
import com.findviewbydream.evil.helloyouth.Fragment.HomeFragmentContent.ShopFragment;
import com.findviewbydream.evil.helloyouth.R;
import com.findviewbydream.evil.helloyouth.Utils.SLog;

/********************************************
 * 工程名：HelloYouth
 * 包名：com.findviewbydream.evil.helloyouth.Fragment
 * 时间：16-6-8 下午12:33
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：首页的Fragment 其中包含3个Fragment
 * （中外交流，二手市场，商品中心）
 ********************************************/
public class HomeFragment extends Fragment
{
    private View view;
    private static HomeFragment homeFragment;
    private Bundle savedInstanceState;

    /*底部按钮*/
    private ImageButton bottom_imageButton1;
    private TextView bottom_textView1;
    private LinearLayout bottom_linearLayout1;
    private ImageButton bottom_imageButton2;
    private TextView bottom_textView2;
    private LinearLayout bottom_linearLayout2;
    private ImageButton bottom_imageButton3;
    private TextView bottom_textView3;
    private LinearLayout bottom_linearLayout3;

    private FrameLayout container_main;/*默认的fragment*/

    private Fragment fragment;

    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    private int resFragment;

    /**
     * 入口函数
     *
     * @return
     */
    public static HomeFragment newInstance()
    {
        if (homeFragment == null)
            homeFragment = new HomeFragment();
        return homeFragment;
    }

    /**
     * 改变填充的Fragment
     *
     * @param resFragment
     */
    public static void changeFragment(int resFragment)
    {
        homeFragment.resFragment = resFragment;
        homeFragment.initFragment();
        homeFragment.switchFragment();
    }

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        outState.putInt("resFragment", resFragment);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.home_fragment, container, false);
        initFragment();/*初始化Fragment*/

        /*如果默认fragment为空，则让他为首页默认*/
        if (fragment == null)
        {
            fragment = CommunionFragment.newInstance();
            transaction.add(R.id.container_main, fragment).commit();
        }

        initView(view);/*初始化控件*/
        initBottomButtonData();/*初始化底部按钮数据*/
        setBottom_imageButton_OnClick();/*设置底部按钮监听*/


        return view;
    }

    /**
     * 初始化Fragment
     */
    private void initFragment()
    {
        fragmentManager = getChildFragmentManager();
        transaction = fragmentManager.beginTransaction();
    }

    /**
     * 更换Fragment
     */
    private void switchFragment()
    {
        //switch (contentFragment.getArguments().getInt("resFragment"))
        //switch (savedInstanceState.getInt("resFragment"))
        switch (resFragment)
        {
            case R.layout.home_communionfragment:
                switchContent(fragment, CommunionFragment.newInstance());
            break;
            case R.layout.home_secondhandfragment:
                switchContent(fragment, SecondHandFragment.newInstance());
                break;
            case R.layout.home_shopfragment:
                switchContent(fragment, ShopFragment.newInstance());
                break;
        }
    }


    /**
     * 判断fragment是否添加过,如果添加过,就隐藏然后显示,没添加过就添加
     *
     * @param from 旧的fragment
     * @param to   新的fragment
     */
    private void switchContent(Fragment from, Fragment to)
    {
        if (fragment != to)
        {
            fragment = to;
            if (!to.isAdded())
            {    // 先判断是否被add过
                SLog.print("添加");
                transaction.hide(from).add(R.id.container_main, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
            }
            else
            {
                SLog.print("替换");
                transaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }
        }
    }

    /**
     * 初始化控件
     *
     * @param view
     */
    private void initView(View view)
    {
        container_main = (FrameLayout) view.findViewById(R.id.container_main);
        bottom_imageButton1 = (ImageButton) view.findViewById(R.id.bottom_imageButton1);
        bottom_textView1 = (TextView) view.findViewById(R.id.bottom_textView1);
        bottom_linearLayout1 = (LinearLayout) view.findViewById(R.id.bottom_linearLayout1);
        bottom_imageButton2 = (ImageButton) view.findViewById(R.id.bottom_imageButton2);
        bottom_textView2 = (TextView) view.findViewById(R.id.bottom_textView2);
        bottom_linearLayout2 = (LinearLayout) view.findViewById(R.id.bottom_linearLayout2);
        bottom_imageButton3 = (ImageButton) view.findViewById(R.id.bottom_imageButton3);
        bottom_textView3 = (TextView) view.findViewById(R.id.bottom_textView3);
        bottom_linearLayout3 = (LinearLayout) view.findViewById(R.id.bottom_linearLayout3);
    }

    /**
     * 初始化底部按钮数据数据
     */
    private void initBottomButtonData()
    {
        Glide.with(getContext()).load(R.drawable.communion).into(bottom_imageButton1);
        Glide.with(getContext()).load(R.drawable.second_hand).into(bottom_imageButton2);
        Glide.with(getContext()).load(R.drawable.shop).into(bottom_imageButton3);
        bottom_textView1.setText("中外交流");
        bottom_textView2.setText("二手市场");
        bottom_textView3.setText("商品中心");
        bottom_linearLayout1.setBackgroundColor(getResources().getColor(R.color.alphagreen));
    }

    /**
     * 设置底部按钮颜色，活动为true，不活动为false
     * 在setBottom_imageButton_OnClick监听中使用
     */
    private void setBackgroundColor(boolean b1, boolean b2, boolean b3)
    {
        if (b1)
            bottom_linearLayout1.setBackgroundColor(getResources().getColor(R.color.alphagreen));
        else
            bottom_linearLayout1.setBackgroundColor(getResources().getColor(R.color.white));
        if (b2)
            bottom_linearLayout2.setBackgroundColor(getResources().getColor(R.color.alphagreen));
        else
            bottom_linearLayout2.setBackgroundColor(getResources().getColor(R.color.white));
        if (b3)
            bottom_linearLayout3.setBackgroundColor(getResources().getColor(R.color.alphagreen));
        else
            bottom_linearLayout3.setBackgroundColor(getResources().getColor(R.color.white));
    }

    /**
     * 设置底部按钮监听
     */
    private void setBottom_imageButton_OnClick()
    {
        View.OnClickListener onClickListener1 = new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                setBackgroundColor(true, false, false);
                HomeFragment.changeFragment(R.layout.home_communionfragment);
            }
        };
        View.OnClickListener onClickListener2 = new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                setBackgroundColor(false, true, false);
                HomeFragment.changeFragment(R.layout.home_secondhandfragment);
            }
        };
        View.OnClickListener onClickListener3 = new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                setBackgroundColor(false, false, true);
                HomeFragment.changeFragment(R.layout.home_shopfragment);
            }
        };
        bottom_linearLayout1.setOnClickListener(onClickListener1);
        bottom_linearLayout2.setOnClickListener(onClickListener2);
        bottom_linearLayout3.setOnClickListener(onClickListener3);
        bottom_imageButton1.setOnClickListener(onClickListener1);
        bottom_imageButton2.setOnClickListener(onClickListener2);
        bottom_imageButton3.setOnClickListener(onClickListener3);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        fragment = null;
    }
}
